package com.zhonghui.modlules.manage.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.eshop.apiCode.EshopException;
import com.zhonghui.common.eshopUtil.ApiResult;
import com.zhonghui.modlules.order.eshopService.StoreOrderService;
import com.zhonghui.modlules.order.eshopVo.StoreOrderQueryVo;
import com.zhonghui.modlules.order.param.OrderDeliveryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Api(value = "商家管理",tags = "商家管理")
public class ShoperController {

    private final StoreOrderService storeOrderService;


    @ApiOperation(value = "订单详情",notes = "订单详情")
    @GetMapping("/order/detail/{key}")
    public ApiResult<StoreOrderQueryVo> orderDetail(@PathVariable String key){
        if (StrUtil.isEmpty(key)){
            throw new EshopException("参数错误");
        }

        StoreOrderQueryVo storeOrderQueryVo = storeOrderService.getOrderInfo(key, null);
        if (ObjectUtil.isNull(storeOrderQueryVo)){
            throw new EshopException("订单不存在");
        }

        return ApiResult.ok(storeOrderService.handleOrder(storeOrderQueryVo));
    }

    @ApiOperation(value = "订单发货",notes = "订单发货")
    @PostMapping("/order/delivery")
    public ApiResult<Boolean> delivery(@Validated @RequestBody OrderDeliveryParam param){
        storeOrderService.orderDelivery(param.getOrderId(),param.getDeliveryId()
        ,param.getDeliveryName(),param.getDeliveryType());

        return ApiResult.ok();
    }
}
